function save_options() {
    saveOptionsWithTag("input");
    saveOptionsWithTag("select");

    if (typeof webkitNotifications != "undefined") {
        if(document.getElementById("mnoti").checked)
            if(false == (webkitNotifications.checkPermission() == 0))
                webkitNotifications.requestPermission(function(){
                    localStorage["mnoti"] = (webkitNotifications.checkPermission() == 0);
                    showSavedStatusForS(2000);
                });
            else
                showSavedStatusForS(2000);
        else {
            localStorage["mnoti"] = "false";
            showSavedStatusForS(4000);
        }
    }
    optionsSaved();
}
function loadKnownIssues() {
    document.getElementById("KnownIssues").innerHTML = chrome.i18n.getMessage("optLoading");
    var xhr = new XMLHttpRequest();
    xhr.open("GET", "http://code.google.com/p/meebo-notifier/issues/list?can=2&q=&colspec=ID+Type+Status+Priority+Milestone+Owner+Summary&sort=&x=&y=&cells=tiles&mode=grid", true);
    xhr.send();
    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4) {
            var div=document.createElement('div');
            div.innerHTML=xhr.responseText;
            if(div.innerHTML != "") {
                var arrTBody = div.getElementsByTagName("tbody");
                for(var i = 0; i < arrTBody.length; i++) {
                    if(arrTBody[i].id == "cursorarea")
                        document.getElementById("KnownIssues").innerHTML = arrTBody[i].parentElement.innerHTML;
                }

                var arrHREF = document.getElementById("KnownIssues").getElementsByTagName("a");
                for(var j = 0; j < arrHREF.length; j++) {
                    var oldSRC = arrHREF[j].search;
                    arrHREF[j].target = "_blank";
                    arrHREF[j].href = "http://code.google.com/p/meebo-notifier/issues/detail"+oldSRC;
                }
            }else {
                chrome.tabs.create({
                    "url" : "https://code.google.com/p/meebo-notifier/issues/list?can=2&q=&colspec=ID+Type+Status+Priority+Milestone+Owner+Summary&sort=&x=&y=&cells=tiles&mode=grid"
                }, null);
                document.getElementById("KnownIssues").innerHTML = chrome.i18n.getMessage("optErrRequestIssue");
            }
        }
    }
}
function loadReportIssue() {
    document.getElementById("ReportIssue").innerHTML = chrome.i18n.getMessage("optLoading");
    var xhr = new XMLHttpRequest();
    xhr.open("GET", "http://code.google.com/p/meebo-notifier/issues/entry?template=Defect%20report%20from%20user", true);
    xhr.send();
    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4) {
            var div=document.createElement('div');
            div.innerHTML=xhr.responseText;
            if(div.innerHTML != "") {
                var arrTBody = div.getElementsByTagName("form");
                for(var i = 0; i < arrTBody.length; i++) {
                    document.getElementById("ReportIssue").innerHTML = arrTBody[i].outerHTML;
                }
                document.getElementById("ReportIssue").firstChild.target = "_blank";
                document.getElementById("ReportIssue").firstChild.action = "http://code.google.com/p/meebo-notifier/issues/entry.do";
            }else {
                chrome.tabs.create({
                    "url" : "https://code.google.com/p/meebo-notifier/issues/entry?template=Defect%20report%20from%20user"
                }, null);
                document.getElementById("ReportIssue").innerHTML = chrome.i18n.getMessage("optErrRequestReport");
            }
        }
    }
}
function _rowRolloverOn(obj) {
//dummy
}
function _rowRolloverOff(obj) {
//dummy
}
function _showBelow(obj) {
//dummy
}
function restore_options() {
    for(name in localStorage) {
        if(name != "undefined") {
            if(document.getElementById(name)) {
                if(document.getElementById(name).type == "text" || document.getElementById(name).type == "password")
                    document.getElementById(name).value = localStorage[name];
                if(document.getElementById(name).type == "checkbox" && "true" == localStorage[name])
                    document.getElementById(name).checked = true;
            }
        }
        optionsSaved();
    }
}
function showSavedStatusForS(milliSec) {
    if("false" == localStorage["mnoti"]) {
        var status = document.getElementById("status");
        status.style.display = "block";
        setTimeout(function() {
            status.style.display = "none";
            window.location.reload();
        }, milliSec);
    }
    else {
        var notification = webkitNotifications.createNotification(chrome.extension.getURL("images/icon.png"), chrome.i18n.getMessage("optNotTitleInformation"), chrome.i18n.getMessage("optNotMesSaved"));
        notification.show();
        setTimeout(function(){
            notification.cancel();
        }, milliSec);
    }
}
function showMessageStatusForS(milliSec, Message) {
    if("false" == localStorage["mnoti"]) {
        var status = document.getElementById("status");
        status.style.display = "block";
        setTimeout(function() {
            status.innerHTML = Message;
            status.style.display = "none";
            window.location.reload();
        }, milliSec);
    }
    else {
        var notification = webkitNotifications.createNotification(chrome.extension.getURL("images/icon_mess.png"), chrome.i18n.getMessage("optNotTitleInformation"), Message);
        notification.show();
        setTimeout(function(){
            notification.cancel();
        }, milliSec);
    }
}
function saveOptionsWithTag(tagName) {
    var allInputs = document.getElementsByTagName(tagName);
    for(i = 0; i < allInputs.length; i++) {
        if(allInputs[i].type == "text" || allInputs[i].type == "password")
            localStorage[allInputs[i].id] = allInputs[i].value;
        if(allInputs[i].type == "checkbox")
            localStorage[allInputs[i].id] = allInputs[i].checked;
    }
}
function checkCredentials() {
    if( document.getElementById("autologin").checked == true) {
        if(document.getElementById("login").value != "" && document.getElementById("password").value != "") {
            optionsChanged();
            return true;
        }
        document.getElementById("autologin").checked = false;
        showMessageStatusForS(2000, chrome.i18n.getMessage("optErrCred"));
    } else
        optionsChanged();
    return false;
}
function optionsChanged() {
    var objButtons = document.getElementsByTagName("button");
    for(var i = 0; i < objButtons.length; i++)
        objButtons[i].disabled = false;
}
function optionsSaved() {
    var objButtons = document.getElementsByTagName("button");
    for(var i = 0; i < objButtons.length; i++)
        objButtons[i].disabled = true;
}